package src.DAO;

import src.jdbc.PostgreSQL;
import src.Entities.Direccion;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
/**
 *
 * @author Rodrigo
 */
public class DireccionDAO {

    public String deleteDireccion(int direccionId){
        try {
            String sql = "DELETE FROM direccion WHERE direccion_id = ?;";
            PreparedStatement delete = PostgreSQL.getConnection().prepareStatement(sql);
            delete.setInt(1, direccionId);
            return String.valueOf(delete.execute());
        } catch (Exception e) {
            return e.toString();
        }
    }

    public String insertDireccion(Direccion address) {
        try {
            String sql = "INSERT " +
                    "       INTO direccion(direccion_id, ciudad_id, calle, numero, numero_departamento, persona_id)" +
                    "     VALUES (NEXTVAL('direccion_seq'), ?, ?,?,?,?,?)";
            PreparedStatement insert = PostgreSQL.getConnection().prepareStatement(sql);
            insert.setInt(1, address.getDireccionId());
            insert.setInt(2, address.getCiudadId());
            insert.setString(3, address.getCalle());
            insert.setInt(4, address.getNumero());
            insert.setInt(5, address.getNumeroDepartamento());
            insert.setInt(6, address.getPersonaId());
            return String.valueOf(insert.execute());
        } catch (Exception e) {
            return e.toString();
        }
    }

    public ArrayList selectCiudad(String sql) {
        ArrayList results = new ArrayList();
        try {
            Connection con = PostgreSQL.getConnection();
            Statement select = con.createStatement();
            ResultSet query = select.executeQuery(sql);
            while (query.next()) {
                Direccion address = new Direccion(query.getInt("direccion_id"),query.getInt("ciudad_id"),
                                        query.getString("calle"), query.getInt("numero"), query.getInt("numero_departamento"),
                                        query.getInt("persona_id"));
                results.add(address);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return results;
    }

    public String updateDireccion(Direccion address) {
        try {
            String sql = "UPDATE direccion" +
                    "        SET ciudad_id = ?, calle = ?, numero = ?, numero_departamento = ?, persona_id = ?" +
                    "      WHERE direccion_id = ?";
            PreparedStatement update = PostgreSQL.getConnection().prepareStatement(sql);
            update.setInt(1, address.getCiudadId());
            update.setString(2, address.getCalle());
            update.setInt(3, address.getNumero());
            update.setInt(4, address.getNumeroDepartamento());
            update.setInt(5, address.getPersonaId());
            update.setInt(6, address.getDireccionId());
            return String.valueOf(update.execute());
        } catch (Exception e) {
            return e.toString();
        }
    }
}
